package mapPackage;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.zip.ZipEntry;

public class RouteIterator implements Iterator<String> {

	private Enumeration<? extends ZipEntry> enumZipEntry;
	
	private Iterator<? extends String> iterator = null;
	
	private LinkedList<String> listNext = new LinkedList<String>();
	
	private String strStartFilter = null;

	private void getNext() {
		if ( iterator == null ) {
			String nextRoute = null;
			boolean found = false;
			while( enumZipEntry.hasMoreElements() && !found) {
				nextRoute = enumZipEntry.nextElement().getName();
				found = nextRoute.startsWith("R_"+(strStartFilter == null ? "" : strStartFilter));
			}
			if (found) {
				listNext.add(nextRoute.substring(2)); 
			}
		} else {
			String nextRoute = null;
			boolean found = false;
			while ( iterator.hasNext() && !found ) {
				nextRoute = iterator.next();
				found = nextRoute.startsWith(strStartFilter == null ? "" : strStartFilter);
			}
			if (found) {
				listNext.add(nextRoute);
			}
		}
	}
	
	public RouteIterator( Enumeration<? extends ZipEntry> name, String stringStartFilter ) {
		enumZipEntry = name;
		strStartFilter = stringStartFilter;
	}
	
	public RouteIterator( Iterator<? extends String> iterator, String stringStartFilter ) {
		this.iterator = iterator;
		strStartFilter = stringStartFilter;
	}

	public boolean hasNext() {
		getNext();
		return !listNext.isEmpty();
	}

	public String next() {
		getNext();
		String next = null;
		if ( listNext.isEmpty() ) {
			throw new NoSuchElementException();
		} else {
			next = listNext.poll();
		}
		return next;
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}
}
